package net.wanho.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.wanho.constant.AuthConstant;
import net.wanho.dto.RoleAuthDTO;
import net.wanho.entity.RoleAuth;
import net.wanho.mapper.RoleAuthMapper;
import net.wanho.service.RoleAuthService;
import net.wanho.service.RoleService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【role_auth(角色权限表)】的数据库操作Service实现
* @createDate 2025-05-05 02:08:26
*/
@Service
@RequiredArgsConstructor
public class RoleAuthServiceImpl extends ServiceImpl<RoleAuthMapper, RoleAuth> implements RoleAuthService{
    private final RoleService roleService;
    private final RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<Integer> getUserAuthIdList(Integer userId) {
        List<Integer> roleIdList = roleService.getRoleIdListByUserId(userId);

        System.out.println(roleIdList);

        return baseMapper.selectList(new LambdaQueryWrapper<RoleAuth>().in(RoleAuth::getRoleId, roleIdList)).stream()
                .mapToInt(RoleAuth::getAuthId)
                .distinct().boxed()
                .collect(Collectors.toList());
    }

    @Override
    public List<Integer> getUserRoleListById(Integer roleId) {
        LambdaQueryWrapper<RoleAuth> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleAuth::getRoleId, roleId);
        return list(queryWrapper).stream()
                .mapToInt(RoleAuth::getAuthId)
                .distinct()
                .boxed().collect(Collectors.toList());
    }

    @Override
    public void updateRoleAuth(RoleAuthDTO roleAuthDTO) {
        Integer roleId = roleAuthDTO.getRoleId();
        List<Integer> newAuthIds = roleAuthDTO.getAuthIds();

        List<Integer> oldAuthIds = list(new LambdaQueryWrapper<RoleAuth>().eq(RoleAuth::getRoleId, roleId)).stream()
                .mapToInt(RoleAuth::getAuthId)
                .distinct().boxed().collect(Collectors.toList());

        List<Integer> addAuthIds = newAuthIds.stream()
                .filter(authId -> !oldAuthIds.contains(authId))
                .collect(Collectors.toList());

        List<Integer> deleteAuthIds = oldAuthIds.stream()
                .filter(authId -> !newAuthIds.contains(authId))
                .collect(Collectors.toList());

        if(ObjectUtil.isNotEmpty(addAuthIds)) {
            addAuthIds.forEach(authId -> baseMapper.insert(new RoleAuth(roleId, authId)));
        }
        if(ObjectUtil.isNotEmpty(deleteAuthIds)) {
            remove(new LambdaQueryWrapper<RoleAuth>().eq(RoleAuth::getRoleId, roleId)
                    .in(RoleAuth::getAuthId, deleteAuthIds));
        }

        redisTemplate.delete(redisTemplate.keys(AuthConstant.AUTH_PREFIX));
    }
}




